<!-- eslint-disable no-unreachable -->
<template>
    <div>
  
      <el-container>
        <!-- 搜索栏 -->
        <el-header>
          <el-row :gutter="20" style="padding: 5px 20px;">
            <el-col :span="6"><el-input v-model="searchModel.orgDept" placeholder="项目名称" clearable @keyup.enter.native="getOrganizationList" /></el-col>
            <el-col :span="6"><el-button type="primary" round icon="el-icon-search" @click="getOrganizationList">搜索</el-button></el-col>
            <el-col align="right" :span="6"><el-button type="primary" round icon="el-icon-plus" @click="handleDialogVisible(null)">新增</el-button></el-col>
          </el-row>
        </el-header>
  
        <!-- 结果列表 -->
        <el-main>
          <el-table :data="list" style="width: 100%">
            <el-table-column type="index" label="index" width="100">
              <template slot-scope="scope">
                {{ (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column prop="orgId" label="部门Id" width="100" />
            <el-table-column prop="orgDept" label="部门名称" width="150" />
            <el-table-column prop="parentOrg" label="总部名称" width="100" />
            <el-table-column prop="orgRegion" label="部门地址" />
            <el-table-column prop="orgType" label="部门类型" />
            <el-table-column prop="orgStatus" label="部门状态">
              <template slot-scope="scope">
              <el-tag v-if="scope.row.orgStatus === 1">正常</el-tag>
              <el-tag v-else type="danger">无效</el-tag>
            </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  icon="el-icon-edit"
                  @click="handleDialogVisible(scope.row.orgId)"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="deleteOrganization(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
  
        <!-- 分页 -->
        <el-footer>
          <el-pagination
            :current-page="searchModel.pageNo"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="searchModel.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalPage"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-footer>
      </el-container>
  
      <!-- 修改框 -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearForm">
        <el-form ref="formRef" :model="form" :rules="rules">
          <el-form-item prop="orgDept" label="部门名称" :label-width="formLabelWidth">
            <el-input v-model="form.orgDept" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="parentOrg" label="总部名称" :label-width="formLabelWidth">
            <el-input v-model="form.parentOrg" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="orgRegion" label="部门地址" :label-width="formLabelWidth">
            <el-input v-model="form.orgRegion" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="orgType" label="部门类型" :label-width="formLabelWidth">
            <el-input v-model="form.orgType" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="orgStatus" label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.orgStatus"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import organizationRequest from '@/api/organizationManage'
  export default {
    data() {
      return {
        totalPage: 0,
        searchModel: {
          orgDept: '',
          pageNo: 1,
          pageSize: 10
        },
        list: [],
        dialogFormVisible: false, // 对话框显示隐藏
        form: {
          
        },
        formLabelWidth: '150px',
        title: '',
        rules: {
          orgDept: [
            { required: true, message: '请输入角色名', trigger: 'blur' },
            { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getOrganizationList()
    },
    methods: {
      handleSizeChange(pageSize) {
        this.searchModel.pageSize = pageSize
        this.getOrganizationList()
      },
      handleCurrentChange(pageNo) {
        this.searchModel.pageNo = pageNo
        this.getOrganizationList()
      },
      getOrganizationList() {
        // then是调完后的回调方法
        // 在then中把数据传回过来, total 和 list要复制
        organizationRequest.getOrganizationList(this.searchModel).then(response => {
          this.totalPage = response.data.totalPage
          this.list = response.data.rows
        })
      },
      handleDialogVisible(id) {
        if (id == null) {
          this.title = '新增部门'
        } else {
          this.title = '修改部门'
          organizationRequest.getOrganizationById(id).then(response => {
            this.form = response.data
          })
        }
  
        this.dialogFormVisible = true
      },
      clearForm() {
        this.form = {}
        this.$refs.formRef.clearValidate()
      },
      handleSave() {
        // 触发表单验证
        this.$refs.formRef.validate((valid) => {
          if (valid) {
            organizationRequest.saveOrganization(this.form).then(response => {
              // 成功提示
              this.$message({
                message: response.message,
                type: 'success'
              })
              // 关闭表格
              this.dialogFormVisible = false
              // 刷新表格
              this.getOrganizationList()
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteOrganization(organization) {
        this.$confirm('此操作将删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          organizationRequest.deleteOrganizationById(organization.orgId).then(response => {
            this.$message({
              type: 'success',
              message: response.message
            })
            this.getOrganizationList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      timestampToTime(timestamp) {
        timestamp = timestamp ? timestamp : null;
        let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
        let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        return Y + M + D + h + m + s;
      }
    }
  }
  </script>
  
  <style>
    .el-dialog .el-input {
      width: 70%;
    }
  </style>
  